package ${configure.packageName}.${generatorTable.moudleName}.service.${generatorTable.fileDirName};

import com.cyys.common.services.RealizeService;
import ${configure.packageName}.${generatorTable.moudleName}.dao.${generatorTable.fileDirName}.${generatorTable.upperClassName}Dao;
import ${configure.packageName}.${generatorTable.moudleName}.model.${generatorTable.fileDirName}.${generatorTable.upperClassName};
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cyys.common.ql.AjaxJson;
import com.cyys.common.utils.StringUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @description ${generatorTable.tableComment!""}Service层
* @author ${configure.authorName}
* @date ${.now?string('yyyy-MM-dd')}
*/
@Service("${generatorTable.className}Service")
@Transactional(readOnly = true)
public class ${generatorTable.upperClassName}Service extends CyysBaseService<${generatorTable.upperClassName}Dao, ${generatorTable.upperClassName}> {

	/**
     * 保存${generatorTable.tableComment!""}
	 * @param entity
     * @return
     * @throws Exception
	 */
	@Transactional(readOnly = false)
	public AjaxJson save(${generatorTable.upperClassName} entity) throws Exception {
		if (StringUtil.isEmpty(entity.getId())) {
			if (StringUtil.isEmpty(entity.getParentId()) || entity.getParentId().equals("0")) { // 保存根节点
				entity.setParentId("0");
			} else {
				${generatorTable.upperClassName} parent = dao.selectById(entity.getParentId());
				// 修改父类的属性
				parent.setHasChild(1); // 设置有子类
				dao.updateById(parent);
			}
			entity.setHasChild(0);
			dao.insert(entity);
			return success("保存成功");
		} else {
			dao.updateById(entity);
			return success("修改成功");
		}
	}

	/**
     * 删除${generatorTable.tableComment!""}
	 * @param id
     * @return
     * @throws Exception
	 */
	@Transactional(readOnly = false)
	public AjaxJson deleteById(String id) throws Exception {
		if (id == null) {
			return error("ID为空");
		}
		${generatorTable.upperClassName} entity = dao.selectById(id);
		int count = dao.chlidNum(id);
		if (count > 0) {
			return error("有子元素，不允许删除");
		}
		// 修改父类属性
		if (entity.getParentId() != null) {
			${generatorTable.upperClassName} parent = dao.selectById(entity.getParentId());
			int parentCount = dao.chlidNum(entity.getParentId());
			if (parentCount == 0) {
				parent.setHasChild(0);
				dao.updateById(parent);
			}
		}
		dao.deleteById(id);
		return success("删除成功");
	}

	/**
	 * 批量删除${generatorTable.tableComment!""}
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = false)
	public AjaxJson deleteByIds(String ids) throws Exception {
		AjaxJson aj = new AjaxJson();
		if (ids == null) {
			return error("ID为空");
		}
		String[] idList = ids.split(",");

		// 检查批量删除数据是否存在问题
		List<${generatorTable.upperClassName}> entityList = dao.listByIds(idList);

		for (${generatorTable.upperClassName} entity : entityList) {
			int count = dao.chlidNum(entity.getId());
			if (count > 0) {
				return error("有子元素，不允许删除");
			}
		}
		for (${generatorTable.upperClassName} entity : entityList) {
			// 修改父类属性
			if (entity.getParentId() != null) {
				${generatorTable.upperClassName} parent = dao.selectById(entity.getParentId());
				int parentCount = dao.chlidNum(entity.getParentId());
				if (parentCount == 0) {
					parent.setHasChild(0);
					dao.updateById(parent);
				}
			}
		}
		dao.deleteBatchIds(Arrays.asList(idList));
		return success("删除成功");
	}

	/**
     * 根据父级编号获取${generatorTable.tableComment!""}
	 * @param parentId
     * @param isFirst
     * @return
     * @throws Exception
	 */
	public List<${generatorTable.upperClassName}> listByParentId(String parentId, boolean isFirst) throws Exception {
		if (parentId == null) {
			List<${generatorTable.upperClassName}> list = dao.getByParentId(null);
			List<${generatorTable.upperClassName}> returnList = getChlid(list, isFirst);
			return returnList;
		} else {
			return dao.getByParentId(parentId);
		}
	}

	/**
     * 递归获取机构
	 * @param list
     * @param isFirst
     * @return
     * @throws Exception
	 */
	private List<${generatorTable.upperClassName}> getChlid(List<${generatorTable.upperClassName}> list, boolean isFirst) throws Exception {
		List<${generatorTable.upperClassName}> returnList = new ArrayList<${generatorTable.upperClassName}>();
		for (${generatorTable.upperClassName} entity : list) {
			List<${generatorTable.upperClassName}> childrenList = dao.getByParentId(entity.getId());
			if (childrenList != null && childrenList.size() > 0) {
				entity.setChildList(childrenList);
				getChlid(childrenList, isFirst);
			}
			returnList.add(entity);
			if (!entity.getParentId().equals("0") && isFirst)
				break;
		}
		return returnList;
	}

}